home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 August: Tool Chest / Dev.CD Aug 00 TC Disk 1.toast / pc / sample code / files / resolverelativealias / resolverelativealias.p < prev    next >
Encoding:
Text File  |  2000-06-23  |  5.2 KB  |  154 lines

  1. (*
  2.     File:        ResolveRelativeAlias.p
  3.     
  4.     Description:A document contains an alias to a file in a known folder.  The problem
  5.                   is that the document might be on a floppy, and the known folder might
  6.                   be on a drive whose name is unknown.
  7.   
  8.                   For example, say the boot drive will contain "Super Folder" in the root
  9.                   directory, and "Super File" in that folder.  The goal is to create an
  10.                   alias to "Super File" which will work regardless of the drive's name.
  11.   
  12.                 Solution:
  13.   
  14.                   Create an alias to the the target (e.g. "SuperFile") which is relative to
  15.                   the System file.  This presumes that the folder structure of the drive
  16.                   is known, but makes no presumption about the name of the drive.
  17.   
  18.                   This sample tool demonstrates the steps involved in creating and resolving
  19.                   the alias.  Instead of creating a document to store the alias, the tool wimps
  20.                   out and stores the alias in a preference file.
  21.   
  22.                   The tool puts up a StandardGetFile dialog.  Selecting a file creates the
  23.                   preference file containing the relative alias; clicking Cancel instead
  24.                   gets the preference file, resolves it, and prints the vRefNum and name
  25.                   of the target.
  26.   
  27.                 Alternative strategies:
  28.   
  29.                   Well, you could call PBGetVInfo to find the name of the boot volume
  30.                   and then build a full pathname, or SetVol to the root directory of the
  31.                   boot volume and then use a relative pathname.  But both of those
  32.                   solutions are ugly and un-Seven like.
  33.  
  34.     Author:        GR
  35.  
  36.     Copyright:     Copyright: © 1992-1999 by Apple Computer, Inc.
  37.                 all rights reserved.
  38.     
  39.     Disclaimer:    You may incorporate this sample code into your applications without
  40.                 restriction, though the sample code has been provided "AS IS" and the
  41.                 responsibility for its operation is 100% yours.  However, what you are
  42.                 not permitted to do is to redistribute the source as "DSC Sample Code"
  43.                 after having made changes. If you're going to re-distribute the source,
  44.                 we require that you make it clear in the source that the code was
  45.                 descended from Apple Sample Code, but that you've made changes.
  46.     
  47.     Change History (most recent first):
  48.                 6/25/99    Updated for Metrowerks Codewarror Pro 2.1(KG)
  49.                 3/92    Created(GR)
  50.  
  51. *)
  52.  
  53. PROGRAM resolveRelativeAlias;
  54.  
  55. USES     Quickdraw,
  56.         Fonts,
  57.         Processes, 
  58.         Files, 
  59.         Aliases, 
  60.         Packages, 
  61.         Script, 
  62.         Resources, 
  63.         Errors, 
  64.         StandardFile,
  65.         Folders;
  66.  
  67.     VAR
  68.         retCode: OSErr;
  69.         mySFR: StandardFileReply;
  70.         mySFTypeList: SFTypeList;
  71.         sysVRefNum, prefVRefNum, myResRefNum: Integer;
  72.         sysDirID, prefDirID: LongInt;
  73.         resolvedFSSpec, sysFSSpec, fileFSSpec: FSSpec;
  74.         targetAliasHandle: AliasHandle;
  75.         changeFlag: Boolean;
  76.     
  77.     PROCEDURE Fail(msg: Str255; retCode: OSErr);
  78.     BEGIN
  79.         WriteLn(msg, ' failed: ', retCode);
  80.         
  81.     END;
  82.  
  83.     PROCEDURE PrintFSSpec(theFSSpec: FSSpec);
  84.     BEGIN
  85.         WriteLn('FSSpec: ', theFSSpec.vRefNum, ' ', theFSSpec.parID, ' ',
  86.             theFSSpec.name);
  87.     END;
  88.  
  89. BEGIN
  90.     InitGraf(@qd.thePort);
  91.     InitFonts;
  92.        InitWindows;
  93.        TEInit;
  94.        InitDialogs (nil);
  95.  
  96.     { find the vRefNum and dirID of the system and preferences folders }
  97.     retCode := FindFolder(kOnSystemDisk, kSystemFolderType, false,
  98.         sysVRefNum, sysDirID);
  99.     IF retCode <> noErr THEN Fail('FindFolder', retCode);
  100.  
  101.     retCode := FindFolder(kOnSystemDisk, kPreferencesFolderType, true,
  102.         prefVRefNum, prefDirID);
  103.     IF retCode <> noErr THEN Fail('FindFolder2', retCode);
  104.  
  105.     { make a FSSpec for the system folder }
  106.     retCode := FSMakeFSSpec(sysVRefNum, sysDirID, 'System', sysFSSpec);
  107.     IF retCode <> noErr THEN Fail('FSMakeFSSpec', retCode);
  108.         
  109.     { get a file to point the alias at }
  110.     StandardGetFile(nil, -1, @mySFTypeList, mySFR);
  111.     
  112.     IF mySFR.sfGood THEN { got a file }
  113.         BEGIN
  114.             { make an alias to the file relative to the system file }
  115.             retCode := NewAlias(@sysFSSpec, mySFR.sfFile, targetAliasHandle);
  116.             IF retCode <> noErr THEN Fail('NewAlias', retCode);
  117.             
  118.             { create the preferences file }
  119.             retCode := FSMakeFSSpec(prefVRefNum, prefDirID, 'My Alis Preference', fileFSSpec);
  120.             FSpCreateResFile(fileFSSpec, '????', 'pref', smRoman);
  121.             retCode := ResError;
  122.             IF retCode <> noErr THEN Fail('FSpCreateResFile', retCode);
  123.             
  124.             { add the alias to the preferences file }
  125.             myResRefNum := FSpOpenResFile(fileFSSpec, fsRdWrPerm);
  126.             IF myResRefNum = -1 THEN Fail('FSpOpenResFile', ResError);
  127.  
  128.             AddResource(Handle(targetAliasHandle), rAliasType, 128,'');
  129.             WriteResource(Handle(targetAliasHandle));
  130.             CloseResFile(myResRefNum);
  131.             WriteLn('done');
  132.         END
  133.     ELSE { user clicked cancel, get the alias from preferences and resolve it }
  134.         BEGIN
  135.             { open the preferences file and get the alias }
  136.             retCode := FSMakeFSSpec(prefVRefNum, prefDirID,
  137.                                     'My Alis Preference', fileFSSpec);
  138.             IF retCode <> noErr THEN Fail('FSMakeFSSpec', retCode);
  139.  
  140.             myResRefNum := FSpOpenResFile(fileFSSpec, fsRdPerm);
  141.             IF myResRefNum = -1 THEN Fail('FSpOpenResFile', ResError);
  142.  
  143.             targetAliasHandle := AliasHandle(GetResource(rAliasType, 128));
  144.             IF targetAliasHandle = NIL THEN Fail('GetResource', ResError);
  145.             
  146.             { resolve the alias relative to the system file }
  147.             retCode := ResolveAlias(@sysFSSpec, targetAliasHandle, resolvedFSSpec, changeFlag);
  148.             IF retCode <> noErr THEN Fail('ResolveAlias', retCode);
  149.             
  150.             Write('resolves to: ');
  151.             PrintFSSpec(resolvedFSSpec);
  152.             CloseResFile(myResRefNum);
  153.         END;
  154. END.